module.exports = function ($scope, $modalInstance, FileUploader,$resource, $modal, item, flag, toaster){
    
  $scope.vm = {
    'date' : '',
    'options' : {
        // format: "YYYY-MM-DD",
        locale : 'zh-cn'
    }
  }
  
  init();
  function init(){
      $scope.opeinfo = flag ? {'flag': true} : angular.copy(item);
      $scope.vm.date = str2date($scope.opeinfo.over_time)
      console.log($scope.opeinfo);
      $resource('/api/as/mc/shopproduct/findMyProductNoPageList', {}, {}).save({}, function (res) {
    if (res.errcode === 0) {
      $scope.ProductList = res.data;
    } else {
      toaster.error({ title: '', body: res.errmsg });
    }
  });
      
  }
 
  //保存
  $scope.save = function (){
      var url =  flag ? '/api/as/mc/shopproduct/createskflashsale' : '/api/as/mc/shopproduct/updateflashsaleInfo';
      if(flag){
          $scope.opeinfo.product_code = $scope.opeinfo.product.product_code;
          // $scope.opeinfo.product = "";
      }
      $scope.opeinfo.over_time = time2str($scope.vm.date._d);
      $resource(url, {}, {}).save($scope.opeinfo, function(res){
          if(res.errcode === 0){
              toaster.success({title: '', body: '保存成功'});
              $modalInstance.close();
          }else{
              toaster.error({title: '', body: res.errmsg});
          }
      });
  }




  //取消
  $scope.cancel = function () {
      $modalInstance.dismiss('cancel');
  }


  function time2str(objDate) {
    if(angular.isDate(objDate))
    {
        var y = objDate.getFullYear();
        var m = objDate.getMonth();
        var d = objDate.getDate();
        var h = objDate.getHours();
        var mt = objDate.getMinutes();
        // var s = objDate.getSeconds();
        return y + '-' + (m + 1) + '-' + d + ' ' + h + ':' + mt;
    }
    else
    {
        return '错误格式';
    }
  }
  
  function str2date (strDate){
  
    if(angular.isString(strDate))
    {
      var objDate = new Date(Date.parse(strDate.replace(/-/g, "/")));
  
      return objDate;
    }
    else
    {
      return '错误格式';
    }
    
  }
  
  var uploader = $scope.uploader = new FileUploader({
    url: 'https://txy.juyouhx.com/Api/Api/ObjectToOss?topdir=aa&selfdir=bb'
  });
  
  uploader.filters.push({
    name: 'imageFilter',
    fn: function(item /*{File|FileLikeObject}*/, options) {
        var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
        return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
    }
  });    
  uploader.onSuccessItem = function(fileItem, response, status, headers) {
    $scope.opeinfo.img = response.savename; 
  
  };
  
  
};

